<?php
namespace Admin\Controller;

use Common\Model\TreeModel;
use Common\Util\CmsData;

class CmsArticleController extends AdminController{

	var $table_category = '';

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$this->assign('meta_title', '文章管理');
		$t_cms_article = M("user_cms_article");
		$map = [];
		$gname = trim($_GET['name']);
		if(isset($gname) && !empty($gname)){
			$map['a.title'] = ['like', "%$gname%"];
		}
		if(isset($_GET['article_status']) && !empty($_GET['article_status'])){
			if($_GET['article_status'] == -1){
				$map['a.article_status'] = ['eq', 0];
			}else{
				$map['a.article_status'] = ['eq', $_GET['article_status']];
			}
		}
		$page = I('page', 1, 'intval'); // 默认显示第一页数据
		$row = 20;
		$data = $t_cms_article
			->alias('a')
			->join('left join '.C('DB_PREFIX').'user_card_list'.' u on u.user_id = a.user_id')
			->where($map)
			->field("a.*,c_touxiang,c_cn_true_name,c_phone,cate_id")
			->order('a.id DESC')
			->page($page, $row)
			->select();
		foreach($data as $key => $val){
			$cate_info = CmsData::getCateInfo($val['cate_id']);
			$data[$key]['cate_name'] = $cate_info['cate_name'];
		}
		$count = $t_cms_article
			->alias('a')
			->join('left join '.C('DB_PREFIX').'user_card_list'.' u on u.id = a.user_id')
			->field("a.*,c_touxiang,c_cn_true_name,c_phone")
			->where($map)
			->count();
		$list_data ['list_data'] = $data;
		// 分页
		if($count > $row){
			$page = new \Think\Page ($count, $row);
			$page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$list_data ['_page'] = $page->show();
		}
		//设置当前url
		$this->assign('all_count', $count);
		$this->assign('list_data', $list_data);
		$this->assign('gget', $_GET);
		$this->display();
	}

	//显示更新
	public function updateShow(){
		$t_cms_article = M("user_cms_article");
		$type = $_GET['type'];
		$val = $_GET['val'];
		$id = $_GET['id'];
		if(!is_numeric($id) || $id < 1){
			echo 0;
			exit();
		}
		$u_data['id'] = $id;
		$u_data[$type] = $val;
		if($t_cms_article->save($u_data)){
			echo 1;
			exit();
		}else{
			echo 0;
			exit();
		}
	}

	public function edit(){
		$t_cms_article = M("user_cms_article");
		$id = I('get.id', 0);
		if(checkSqlID($id) == false){
			$this->error(0, '信息不存在');
		}
		$s_map['id'] = $id;
		$info = $t_cms_article->where($s_map)->find();
		if(IS_POST){
			$go_data['article_status'] = $_POST['article_status'] ? $_POST['article_status'] : 0;
			$go_data['update_time'] = time();
			$id = $_POST['id'];
			if($id > 0){
				$smap['id'] = $id;
				if(!$t_cms_article->where($smap)->save($go_data)){
					$this->return_json_data(0, '对不起，更新失败，请重试');
				}
				$insert_id = $id;
			}else{
				$go_data['add_time'] = time();
				if(!$insert_id = $t_cms_article->add($go_data)){
					$this->return_json_data(0, '对不起，添加失败，请重试');
				}
			}
			if($insert_id){
				$this->return_json_data(1, '信息更新成功');
			}else{
				$this->return_json_data(1, '信息更新失败，请重试');
			}
		}
		$cate_info = CmsData::getCateInfo($info['cate_id']);
		$info['cate_name'] = $cate_info['cate_name'];
		$info['content'] = $info['content'] ? json_decode($info['content'], true) : [];
		$this->assign('info', $info);
		$this->display('edit');
	}

	//删除
	public function del(){
		$id = trim($_GET['id']);
		if(!is_numeric($id) || $id < 0){
			$this->error('非法操作');
		}
		$t_cms_article = M("user_cms_article");
		$map['id'] = $id;
		if($t_cms_article->where($map)->save(['is_delete' => 1, 'update_time' => time()])){
			$this->success('删除成功');
		}else{
			$this->error('删除失败，请重试');
		}
	}

	/**
	 * 批量操作
	 */
	public function actionAllGoods(){
		$app_id = $this->this_app_id;
		$t_shop_goods = M("tuan_shop_goods");
		$ids = $_GET['ids'] ? trim($_GET['ids']) : '';
		$atype = $_GET['atype'] ? trim($_GET['atype']) : '';
		if(empty($ids)){
			$this->return_json_data(0, '请选择要操作的商品');
		}
		if(empty($atype)){
			$this->return_json_data(0, '请选择要执行的操作');
		}
		$ids = substr($ids, 0, -1);
		$gmap['id'] = ['in', $ids];
		$gmap['app_id'] = $app_id;
		$gmap['tuan_daili_id'] = $this->this_tuan_daili_id;
		if($atype == 'shangjia'){
			$t_shop_goods->where($gmap)->save(['is_shangjia' => 1, 'update_time' => time()]);
		}elseif($atype == 'xiajia'){
			$t_shop_goods->where($gmap)->save(['is_shangjia' => 0, 'update_time' => time()]);
		}elseif($atype == 'delete'){
			$t_shop_goods->where($gmap)->delete();
		}
		$this->return_json_data(1, '操作成功');
	}

}
